capture log close
log using C:\data\UI_2020\logs\regs_basic_rev3_new.log, replace
*************************************************************************
* Created by: 	Rob Valletta						
* Created on: 	05/15/2022
* Description: 	Regs of transition rates on controls, UI replacement rates.
*		UI rep rates calculated at individual level using CPS ASEC 
*		and Ganong et al. calculator.
*		Regressions used matched panel for Jan.-Jul 2020
*		(restricted to those with ASEC info)
* Modified on:	Based on earlier "regs_paper_5lw" (use longitudinal weights)
*		Include EU and EN regs, other small changes (commented)
*		6/6/22: Include majocc dummies to this version "2"
* 		6/18/22: Use new "v2" data set (small obs increase)
*		2/3/23: Exclude armed forces
*		3/6/23: Import new state unemp/emp data, make sure matched to base month (t-1, not t)
*************************************************************************

version 15
clear *
set more off
capture program drop _all
cd C:\data\UI_2020
set linesize 120

** Pull in analysis extract 
use data\CPS_2020_unbalanced_v2.dta, clear

** (DROP THIS STEP HERE) Reset month to match forward month (through Jul 2020)
** replace month=month+1

* FORM UI REPLACEMENT RATE (WITH MISSINGS), DROP 0'S and DC
gen UI_rep=(UI_CARES/weekly_earnings)
  replace UI_rep=0 if UI_rep==.
drop if UI_rep==0
drop if st=="DC"

** DEFINE SOME MACROS
macro define dur_FV i.durmoncat
macro define dur_JR dur dur_sq dur_inv new_un gte26
macro define X_p i.educ i.agecat female married fem_marr i.racegrp
macro define X_s urate* d3lne*

egen double pid=group(peridnum)
xtset pid mis
gen pwlgwgt_b=f.pwlgwgt if matchf==1
  replace pwlgwgt=pwlgwgt_b

** FORM CORRECTED MAJOCC
gen majocc2=majocc
  replace majocc2=l.majocc2 if (majocc<0 & lfstat2==2 & l.lfstat2==2)

** SET NEG MAJIND AND MAJOCC TO MISSING
replace majind=. if majind<=0
replace majind2=. if majind2<=0
replace majocc=. if majocc<=0
replace majocc2=. if majocc2<=0

** DROP FLAGGED BAD MATCHES
drop if fl_bad_cps==1

** DROP ARMED FORCES
drop if (majind==14 | majocc==11)

** Drop state vars, create year var for merge
drop emp-d12lne urate*
gen year=2020

** Update and re-match: CURRENT MONTH
  ** (base month for transition)
sort year month state
merge m:1 year month state using data\emp_urate_lf_state, keep(1 3) nogen

** NOW RE-SET MONTH (forward transtions identified by outcome month)
replace month=month+1

** For pre/post design, interact UI_rep with Mar-Aug. dummies
gen UI_rep_Mar=UI_rep*(month==3)
gen UI_rep_Apr=UI_rep*(month==4)
gen UI_rep_May=UI_rep*(month==5)
gen UI_rep_Jun=UI_rep*(month==6)
gen UI_rep_Jul=UI_rep*(month==7)

** Form urate and d3lne cubics
gen urate2=urate^2
gen urate3=urate^3
gen d3lne2=d3lne^2
gen d3lne3=d3lne^3

** REGRESSIONS HERE (some sample changes)
preserve

** UE unrestricted
keep if lfstat==2
logit UEf UI_rep* i.month $dur_FV $X_p $X_s i.majind i.majocc i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo UE_1f

** EU unrestricted (drop dur)
restore, preserve
keep if lfstat==1
logit EUf UI_rep* i.month $X_p $X_s i.majind i.majocc i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo EU_1f

** EN unrestricted (drop dur)
restore, preserve
keep if lfstat==1
logit ENf UI_rep* i.month $X_p $X_s i.majind i.majocc i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo EN_1f

** UE match2f
  ** SLIGHT CHANGE FROM PREVIOUS: Included "restore, preserve" here to account for lfstat/lfstat2 diffs (as in next reg)
restore, preserve
keep if lfstat2==2 & match2f==1
logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo UE_2f
  ** Check monthly sample count
tab month if e(sample)

** Get means of exit2_emp, UI_rep
summ exit2_emp UI_rep if e(sample) [aw=pwlgwgt]
summ exit2_emp UI_rep if (e(sample) & month>3) [aw=pwlgwgt]

** UN
restore, preserve
keep if lfstat2==2 & match2f==1
logit exit2_nilf UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo UN_2f

** NE (drop dur, majind2)
restore, preserve
keep if N==1
logit NEf UI_rep* i.month $X_p $X_s i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo NE_1f


** Balanced sample
** Pull in analysis extract 
use data\CPS_2020_balanced_v2.dta, clear

* FORM UI REPLACEMENT RATE (WITH MISSINGS), DROP 0'S and DC
gen UI_rep=(UI_CARES/weekly_earnings)
  replace UI_rep=0 if UI_rep==.
  drop if UI_rep==0
drop if st=="DC"

** DEFINE SOME MACROS
** Drop "i.racegrp" for now (var not in data)
macro define dur_FV i.durmoncat
macro define dur_JR dur dur_sq dur_inv new_un gte26
macro define X_p i.educ i.agecat female married fem_marr i.racegrp
macro define X_s urate* d3lne*

egen double pid=group(peridnum)
xtset pid mis
gen pwlgwgt_b=f.pwlgwgt if matchf==1
  replace pwlgwgt=pwlgwgt_b

** FORM CORRECTED MAJOCC
gen majocc2=majocc
  replace majocc2=l.majocc2 if (majocc<0 & lfstat2==2 & l.lfstat2==2)

** SET NEG MAJIND AND MAJOCC TO MISSING
replace majind=. if majind<=0
replace majind2=. if majind2<=0
replace majocc=. if majocc<=0
replace majocc2=. if majocc2<=0

** DROP FLAGGED BAD MATCHES
drop if fl_bad_cps==1

** DROP ARMED FORCES
drop if (majind==14 | majocc==11)

** Drop state vars, create year var for merge
drop emp-d12lne urate*
gen year=2020

** Update and re-match: CURRENT MONTH
  ** (base month for transition)
sort year month state
merge m:1 year month state using data\emp_urate_lf_state, keep(1 3) nogen

** NOW RE-SET MONTH (forward transtions identified by outcome month)
replace month=month+1

** For pre/post design, interact UI_rep with Mar-Aug. dummies
gen UI_rep_Mar=UI_rep*(month==3)
gen UI_rep_Apr=UI_rep*(month==4)
gen UI_rep_May=UI_rep*(month==5)
gen UI_rep_Jun=UI_rep*(month==6)
gen UI_rep_Jul=UI_rep*(month==7)

** Form urate and d3lne cubics
gen urate2=urate^2
gen urate3=urate^3
gen d3lne2=d3lne^2
gen d3lne3=d3lne^3

** Balanced sample reg
keep if lfstat2==2 & match2f==1
logit exit2_emp UI_rep* i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust)
margins, dydx(UI_rep*) post
eststo UE_ba

** Export results using estout
#delimit ;
esttab UE_2f UE_1f UE_ba UN_2f NE_1f EU_1f EN_1f using output\regs_basic_rev3_new, 
csv keep(UI_rep*) cells(b(vacant(-) star fmt(3)) se(fmt(3) par)) label
mlabels(dep) collabels(none) eqlabels(none)  
starlevels(* 0.10 ** 0.05 *** .01) replace;
#delimit cr


** Produce 90/10 UI_rep plot (column 1 spec)
** Pull in analysis extract 
use data\CPS_2020_unbalanced_v2.dta, clear

* FORM UI REPLACEMENT RATES (WITH MISSINGS), DROP 0'S and DC
gen UI_rep=(UI_CARES/weekly_earnings)
  replace UI_rep=0 if UI_rep==.
drop if UI_rep==0
gen UI_rep_pre=(UI/weekly_earnings)
  replace UI_rep_pre=0 if UI_rep==.
drop if st=="DC"

** DEFINE SOME MACROS
macro define dur_FV i.durmoncat
macro define dur_JR dur dur_sq dur_inv new_un gte26
macro define X_p i.educ i.agecat female married fem_marr i.racegrp
macro define X_s urate* d3lne*

egen double pid=group(peridnum)
xtset pid mis
gen pwlgwgt_b=f.pwlgwgt if matchf==1
  replace pwlgwgt=pwlgwgt_b

** FORM CORRECTED MAJOCC
gen majocc2=majocc
  replace majocc2=l.majocc2 if (majocc<0 & lfstat2==2 & l.lfstat2==2)

** SET NEG MAJIND AND MAJOCC TO MISSING
replace majind=. if majind<=0
replace majind2=. if majind2<=0
replace majocc=. if majocc<=0
replace majocc2=. if majocc2<=0

** DROP FLAGGED BAD MATCHES
drop if fl_bad_cps==1

** DROP ARMED FORCES
drop if (majind==14 | majocc==11)

** Drop state vars, create year var for merge
drop emp-d12lne urate*
gen year=2020

** Update and re-match: CURRENT MONTH
  ** (base month for transition)
sort year month state
merge m:1 year month state using data\emp_urate_lf_state, keep(1 3) nogen

** NOW RE-SET MONTH (forward transtions identified by outcome month)
replace month=month+1

** For pre/post design, interact UI_rep with Mar-Aug. dummies
gen UI_rep_Mar=UI_rep*(month==3)
gen UI_rep_Apr=UI_rep*(month==4)
gen UI_rep_May=UI_rep*(month==5)
gen UI_rep_Jun=UI_rep*(month==6)
gen UI_rep_Jul=UI_rep*(month==7)

** Form urate and d3lne cubics
gen urate2=urate^2
gen urate3=urate^3
gen d3lne2=d3lne^2
gen d3lne3=d3lne^3

** Check UI_rep values in full U==1 sample
  ** Post values
summ UI_rep [aw=pwlgwgt] if U==1, detail
  ** Pre values
summ UI_rep_pre [aw=pwlgwgt] if U==1, detail

** NOTE: in earlier program, this sample restriction was slightly off
** UE match2f
keep if lfstat2==2 & match2f==1
  ** NOTE: Need to make sure UI_rep_pre not included here, so don't use "UI_rep*"
logit exit2_emp UI_rep UI_rep_Mar-UI_rep_Jul i.month $dur_FV $X_p $X_s i.majind2 i.majocc2 i.state [iw=pwlgwgt], vce(robust)
keep if e(sample)

** Get UI_rep means
  ** Full sample first, post and pre
summ UI_rep [aw=pwlgwgt], detail
  scalar UI_pst=r(p50)
summ UI_rep_pre [aw=pwlgwgt], detail
  scalar UI_pre=r(p50)
  ** Now do for post/pre months separately (for elasticity calculations)
summ UI_rep if (month>3) [aw=pwlgwgt], detail
summ UI_rep_pre if (month<4) [aw=pwlgwgt], detail

   
** Get predictions at pre/pst medians
replace UI_rep=UI_pst
replace UI_rep_Mar=UI_rep*(month==3)
replace UI_rep_Apr=UI_rep*(month==4)
replace UI_rep_May=UI_rep*(month==5)
replace UI_rep_Jun=UI_rep*(month==6)
replace UI_rep_Jul=UI_rep*(month==7)
predict UEf_pst

replace UI_rep=UI_pre
replace UI_rep_Mar=UI_rep*(month==3)
replace UI_rep_Apr=UI_rep*(month==4)
replace UI_rep_May=UI_rep*(month==5)
replace UI_rep_Jun=UI_rep*(month==6)
replace UI_rep_Jul=UI_rep*(month==7)
predict UEf_pre

** Take monthly means
collapse UEf_p* [aw=pwlgwgt], by(month)
tsset month

list

label var UEf_pst "Post-CARES median"
label var UEf_pre "Pre-CARES median"

** Plot
#d ;
** UE rates;
twoway  (tsline UEf_pre, lpattern(solid) lwidth(medthick) lcolor(black))
	(tsline UEf_pst, lpattern(solid) lwidth(medthick) lcolor(red)),
title("Job finding rates (from unemployment), by UI rep rates (pre/pst CARES)", size(medsmall)) 
xtitle("") xscale(range(2 6.2)) xline(3.5, lcolor(gray)) text(.16 3.2 "$600 Supp Enacted", size(vsmall))
legend(col(1) pos(5) ring(0) size(vsmall) rowgap(*0) colgap(*.5) keygap(*.5))
xlabel(2 "Feb-20" 4 "Apr-20" 6 "Jun-20", tlength(*2) tposition(outside) labgap(0.5) labs(small))
ytitle("Rate", margin(small) size(small)) yscale(range(0.15 0.45))
ylabel(0.15(0.05)0.45, labs(small)) plotregion(margin(0))
note("Note: Logit regression results using matched CPS microdata, Jan-Jul 2020, linked to CPS"
"ASEC to form UI replacement rates based on 2019 earnings. Flows corrected for reported" 
"transitions out of unemployment followed by a return to unemployment in the next month."
"Additional regression controls include individual and job characteristics, state labor market"
"conditions, and time effects.", size(small));
graph save graphs\UE_rep_pst_pre_regs_rev3_new, replace;

#d cr

log close
